package com.mall.product.controller;

import com.mall.common.utils.BaseResult;
import com.mall.common.utils.PageUtils;
import com.mall.product.entity.ProductAttrValueEntity;
import com.mall.product.service.AttrService;
import com.mall.product.service.ProductAttrValueService;
import com.mall.product.vo.AttrGroupRelationVo;
import com.mall.product.vo.AttrRespVo;
import com.mall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 商品属性
 *
 * @author rose
 * @email zichuanhaoyue@gmail.com
 * @date 2021-05-16 19:10:47
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService attrValueService ;

    @GetMapping("/base/listforspu/{spuId}")
    public BaseResult baseAttrListforspu(@PathVariable("spuId") Long spuId){

        List<ProductAttrValueEntity> page = attrValueService.baseAttrListforspu(spuId);

        return BaseResult.ok().put("data", page);
    }


    /**
     *
     * http://localhost:88/api/product/attr/base/list/0?t=1696086968545&page=1&limit=10&key=
     * 请求方法: GET
     *
     * @return
     */
    @GetMapping("/{attrType}/list/{catelogId}")
    public BaseResult baseListAttr(@RequestParam Map<String, Object> params ,
                                   @PathVariable("catelogId") Long catelogId,
                                   @PathVariable("attrType") String attrType){

        PageUtils page = attrService.queryPageByCondition(params, catelogId,attrType);

        return BaseResult.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public BaseResult list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return BaseResult.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public BaseResult info(@PathVariable("attrId") Long attrId){

		// AttrEntity attr = attrService.getById(attrId);

        AttrRespVo attr = attrService.getAttrInfo(attrId);

        return BaseResult.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public BaseResult save(@RequestBody AttrVo attr){


		attrService.save(attr);

        return BaseResult.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public BaseResult update(@RequestBody AttrVo attr){


		// attrService.updateById(attr);
		attrService.updateAttr(attr);

        return BaseResult.ok();
    }

    @PostMapping("/update/{spuId}")
    public BaseResult updateSpuAttr(@RequestBody List<ProductAttrValueEntity> attrs,@PathVariable("spuId") Long spuId){

        attrValueService.updateSpuAttr(attrs,spuId);

        return BaseResult.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public BaseResult delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return BaseResult.ok();
    }

    @PostMapping("/attr/relation/delete")
    public BaseResult deleteRelation(@RequestBody AttrGroupRelationVo[] vos){

        attrService.deleteRelation(vos);

        return BaseResult.ok();
    }

}
